<template>
  <div class="login_container">
    <div class="login_box">
      <!-- logo区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <!--注册区域 -->
      <el-form ref="loginFormRef" label-width="0px" class="login_form" :model="registerForm" :rules="loginRule">
       
        <!-- 用户名 -->
        <el-form-item prop="userId">
          <el-input placeholder="请输入用户名" prefix-icon="el-icon-s-custom" v-model="registerForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" prefix-icon="el-icon-right" v-model="registerForm.password"
          type="password"></el-input>
        </el-form-item>

        <el-form-item class="buttons">
          <el-button type="primary" @click="login">注册</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button> 
        </el-form-item>
      </el-form>
    </div>
  </div>
  
</template>

<script>

export default {
  data(){
    return{
        registerForm:{
            userId: 1,
            username:"zhangsan",
            password:"123456"
        },
        
        //表单验证规则
        loginRule:{
            //用户Id验证
            userId:[
                { required: true, message: '此项必填', trigger: 'blur' }
            ],
            //密码验证
            password:[
                { required: true, message: '此项必填', trigger: 'blur' }
            ]  
        }
    }
  },
  methods:{
    //重置表单
    resetLoginForm(){
        this.$refs.loginFormRef.resetFields(); 
    },
    login(){
        this.$refs.loginFormRef.validate(async valid=>{
        
            if(!valid) return;

            // 为了能够使用application/x-www-form-urlencoded来传递数据，也是费尽了心机啊
            let param = new URLSearchParams();
            param.append('username', this.registerForm.username);
            param.append('password', this.registerForm.password);
            const {data:result}=await this.$axios({
                method: 'post',
                url: 'register',
                data: param
            });

            //判断data中的内容是否为空，如果为空代表这个用户不存在。
            var arr = Object.keys(result);
            if(arr.length == 0){
                this.$message.success("注册失败");
                return;
            }
          
            this.$message.success("注册成功");
            window.sessionStorage.setItem("token",result.id);
            this.$router.push('/home');

        });
    }
  }
  
}
</script>

<style lang="less" scoped>
.login_container{
  background-color: #2b4b6b;
  height: 100%;
}

.login_box{
  height: 300px;
  width: 450px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);

  .avatar_box{
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 10%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;

    img{
      height: 100%;
      width: 100%;
      background-color: #eee;
      border-radius: 10%;
    }
  }
}

.buttons{
  display: flex;
  justify-content: flex-end;
}

.login_form{
  position: absolute;
  bottom:0px;
  width: 100%;
  padding:0px 40px;
  
  box-sizing: border-box;
}
</style>